package com.jiajia.study.ware.service.impl;

import com.jiajia.study.ware.constant.WareConstant;
import com.jiajia.study.ware.entity.PurchaseEntity;
import com.jiajia.study.ware.vo.MergeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.Query;

import com.jiajia.study.ware.dao.PurchaseDetailDao;
import com.jiajia.study.ware.entity.PurchaseDetailEntity;
import com.jiajia.study.ware.service.PurchaseDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(a->{
                a.eq("purchase_id",key)
                        .or().eq("sku_id",key);
            });
        }

        String status = (String) params.get("status");
        if(!StringUtils.isEmpty(status)){
            queryWrapper.and(a-> {
                a.eq("status",status);
            });
        }


        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(status)){
            queryWrapper.and(a-> {
                a.eq("ware_id",wareId);
            });
        }



        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateBatchStatus(List<Long> purchaseIds) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("purchase_id",purchaseIds);
        List<PurchaseDetailEntity> list = this.list(queryWrapper);

        List<PurchaseDetailEntity> updateList = list.stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
            purchaseDetailEntity.setId(item.getId());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());

        this.updateBatchById(updateList);
    }


}